import axios from 'axios'
import { BASE_URL } from '../constants/default.js'
import role from '@/store/role'
axios.defaults.timeout = 15000
// axios.defaults.headers.common['apifoxToken'] = 'tCW67fZcy5PTx7JU2S7XcOylCfDAMFTC'
axios.defaults.headers.common['usertoken'] = window.localStorage.getItem('usertoken')

export function setToken(usertoken) {
  axios.defaults.headers.common['usertoken'] = usertoken
  window.localStorage.setItem('usertoken', usertoken)
  setRole(usertoken)
}

export function setRole(token) {
  const rolePinia = role()
  rolePinia.update(token)
}

export function get(path) {
  return new Promise((res, rej) => {
    const url = `${BASE_URL}${path}`
    axios
      .get(url)
      .then(data => {
        res(data)
      })
      .catch(reason => {
        rej(reason)
      })
  })
}

export function post(path, postData) {
  return new Promise((res, rej) => {
    const url = `${BASE_URL}${path}`
    axios
      .post(url, postData)
      .then(data => {
        res(data)
      })
      .catch(reason => {
        rej(reason)
      })
  })
}
